import {Component, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {ExtendHttpService} from "../../common/extend-http.service";
import {TipService} from "../../main/services/tip.service";
import {SocketService} from "../../main/services/socket.service";
import {IndexService} from "../../main/services/index.service";

@Component({
  selector: 'app-choose-shop',
  templateUrl: './choose-shop.component.html',
  styleUrls: ['./choose-shop.component.css']
})
export class ChooseShopComponent implements OnInit {

  // 加载组件状态
  isLoading: boolean = false;

  // 商铺列表
  merchantArr = [];

  // 用户名
  mobile: string;

  // 密码
  login_pwd: string;

  constructor(private _activatedRoute: ActivatedRoute,
              private extendHttp: ExtendHttpService,
              private tipService: TipService,
              private socketService: SocketService,
              private _router: Router,
              private _indexService: IndexService) {
  }

  ngOnInit() {
    let first = null;
    cBack = () => {
      if (!first) {
        first = new Date().getTime();
        setTimeout(() => {
          first = null;
        }, 1000);

      } else {
        if (new Date().getTime() - first < 1000) {
          webview.close()
        }
      }
    }
    this._activatedRoute.queryParams.subscribe(res => {
      this.merchantArr = JSON.parse(res['merchantArr']);
      this.mobile = res['mobile'];
      this.login_pwd = res['login_pwd'];
    })
  }

  // 商户选择
  selectMerchant(merchant) {
    if (!this.isLoading) {
      let params2 = {
        mobile: this.mobile,
        login_pwd: this.login_pwd,
        merchant_id: merchant.merchant_id
      };
      this.isLoading = !this.isLoading;
      this.extendHttp.post('/merchantApp/login', params2).subscribe(response => {
        let res = response.json();
        if (res.code == '0') {
          setTimeout(() => {
            localStorage.setItem('loginInfo', JSON.stringify(res));
            this._router.navigateByUrl('/main?loginFlag=true');
          }, 1000);
        } else {
          this.tipService.change(true, res['message']);
          this.isLoading = !this.isLoading;
        }
      }, error => {
        this.tipService.change(true, '系统繁忙，操作失败！');
        this.isLoading = !this.isLoading;
      });
    }
  }
}
